#FILE: EntityFactoryManager.py
#DESC: Manages a collection of EntityFactory objects
#

import re

from infra.logging import logger

class NoSuchEntityFactory(RuntimeError):
	pass


entityFactories_ = []

def registerFactory(entityFactory):
	log = logger.getLogger()
	log.debug("Registering entityFactory = %s ..." % entityFactory)
	global entityFactories_
	entityFactories_.append(entityFactory)
	log.debug("Factory is registered.")
	

def findFactory(entityType):
	factory = None
	global entityFactories_
	log = logger.getLogger()

	log.debug("Search for factory to create entityType = \"%s\" ..." % entityType)

	for entityFactory in entityFactories_:
		if re.search(entityFactory.getEntityType(), entityType):
			factory = entityFactory
			break

	if not factory:
		raise NoSuchEntityFactory("Could not find a factory for entityType = \"%s\"" % entityType)

	return factory
